package com.atom.test.opengl

import android.app.Activity
import android.content.Intent
import android.opengl.EGL14
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.ScrollView
import com.atom.module.lib.bullet.HelloBulletActivity
import com.atom.module.lib.opengles.egl.EGLHelper
import com.atom.module.lib.opengles.egl.EGLWrapper
import com.blankj.utilcode.util.LogUtils

class MenuActivity : Activity(), View.OnClickListener {

    lateinit var linearLayout: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createContentView())
    }

    private fun createContentView(): View {
        val contentView = FrameLayout(this)
        val scrollView = ScrollView(this)
        scrollView.layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        linearLayout.post {
            buttons.forEach { entry ->
                linearLayout.addView(Button(this).also {
                    it.text = entry
                    it.setOnClickListener(MenuActivity@ this)
                })
            }
        }
        scrollView.addView(linearLayout)
        contentView.addView(scrollView)
        return contentView
    }

    val buttons = arrayListOf<String>(
        "Bullet物理引擎一览",
        "EGL和自定义GlCode",
        "EGL属性打印",
        "基于EGL动态12边形", "基于EGL帧缓存", "基于EGL和ExoPlayer圆角视频",
        "基于MatrixModel矩阵形变和位移", "基于MatrixView矩阵观察变换", "基于MatrixProject矩阵投影变换",
        "基于手势管理FrameLayout子View变换", "基于GLSurfaceView教学",
        "基于FrameCallback回调学习",
        "样板",
        "END"
    )

    override fun onClick(v: View?) {
        if (v is Button) {
            when (v.text) {
                "Bullet物理引擎一览" -> {
                    startActivity(Intent(this@MenuActivity, HelloBulletActivity::class.java))
                }
                "EGL和自定义GlCode" -> {
                    startActivity(Intent(this@MenuActivity, EGLActivity::class.java))
                }
                "EGL属性打印" -> {
                    val eglWrapper: EGLWrapper = EGLHelper.initDisplay()
                    LogUtils.e(
                        "EGL_VENDOR = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_VENDOR
                            )
                        }",
                        "EGL_VERSION = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_VERSION
                            )
                        }",
                        "EGL_EXTENSIONS = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_EXTENSIONS
                            )
                        }",
                        "EGL_RED_SIZE = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_RED_SIZE
                            )
                        }",
                        "EGL_BLUE_SIZE = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_BLUE_SIZE
                            )
                        }",
                        "EGL_GREEN_SIZE = ${
                            EGL14.eglQueryString(
                                eglWrapper.eglDisplay,
                                EGL14.EGL_GREEN_SIZE
                            )
                        }",
                    )
                    EGLHelper.destroy(eglWrapper)
                }
                "基于EGL动态12边形" -> {
                    startActivity(Intent(this@MenuActivity, DrawActivity::class.java))
                }
                "基于EGL帧缓存" -> {
                    startActivity(Intent(this@MenuActivity, FrameActivity::class.java))
                }
                "基于EGL和ExoPlayer圆角视频" -> {
                    val intent = Intent(Intent.ACTION_GET_CONTENT)
                    //intent.setType("image/*");//选择图片
                    //intent.setType("audio/*"); //选择音频
                    //intent.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
                    //intent.setType("video/*;image/*");//同时选择视频和图片
                    //intent.setType("*/*");//无类型限制
                    //intent.setType("image/*");//选择图片
                    //intent.setType("audio/*"); //选择音频
                    //intent.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
                    //intent.setType("video/*;image/*");//同时选择视频和图片
                    //intent.setType("*/*");//无类型限制
                    intent.type = "video/*"
                    intent.addCategory(Intent.CATEGORY_OPENABLE)
                    startActivityForResult(
                        intent,
                        2048
                    )
                }
                "基于MatrixModel矩阵形变和位移" -> {
                    startActivity(Intent(this@MenuActivity, ModelMatrixActivity::class.java))
                }
                "基于MatrixView矩阵观察变换" -> {
                    startActivity(Intent(this@MenuActivity, ViewMatrixActivity::class.java))
                }
                "基于MatrixProject矩阵投影变换" -> {
                    startActivity(Intent(this@MenuActivity, ProjectMatrixActivity::class.java))
                }
                "基于手势管理FrameLayout子View变换" -> {
                    startActivity(Intent(this@MenuActivity, FrameLayoutActivity::class.java))
                }
                "基于GLSurfaceView教学" -> {
                    startActivity(Intent(this@MenuActivity, GLSurfaceViewActivity::class.java))
                }
                "基于FrameCallback回调学习" -> {
                    startActivity(Intent(this@MenuActivity, FrameCallbackActivity::class.java))
                }
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 2048 && resultCode == RESULT_OK) {
            if (data == null) return
            val uri = data.data
            try {
                if (uri != null) {
                    val intent = Intent(this, VideoActivity::class.java)
                    intent.data = uri
                    startActivity(intent)
                }
            } catch (ignored: Exception) {
            }
        }
    }
}